package com.markupartist.android.example.pulltorefresh2;

import java.util.Arrays;
import java.util.LinkedList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

import com.markupartist.ui.PullToRefreshListView;
import com.markupartist.ui.PullToRefreshListView.OnRefreshListener;

public class PullToRefreshActivity extends Activity {
	private static final String TAG = "PullToRefreshActivity";
    private LinkedList<String> mListItems;

    private int refreshCount = 0;
    private  PullToRefreshListView list;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pull_to_refresh);
        
        list = (PullToRefreshListView) findViewById(R.id.list);
        
        // Set a listener to be invoked when the list should be refreshed.
        /*((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
            	Log.i(TAG,"onRefresh()-->");
                new GetDataTask().execute();
            }
        });*/

        list.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
            	Log.i(TAG,"onRefresh()-->");
                new GetDataTask().execute();
            }
        });
        
        mListItems = new LinkedList<String>();
        mListItems.addAll(Arrays.asList(mStrings));

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, mListItems);

        list.setAdapter(adapter);
    }

    private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return mStrings;
        }

        @Override
        protected void onPostExecute(String[] result) {
            mListItems.addFirst("Added after refresh... " + (++refreshCount));

            // Call onRefreshComplete when the list has been refreshed.
            list.onRefreshComplete();
//            list.setSelection(1);

            super.onPostExecute(result);
        }
        
        

        

		@Override
		protected void onPreExecute() {
			list.prepareForRefresh();
			super.onPreExecute();
		}

		@Override
		protected void onProgressUpdate(Void... values) {
//			list.refresh();
			super.onProgressUpdate(values);
		}
        
    }

    private String[] mStrings = {"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam","Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis"};
/*    private String[] mStrings = {
    		"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam"
    		,"Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis"
    		,"Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
    "Allgauer Emmentaler"};
*/}
